<template>
  <div class="login">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login-form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1 class="unselectable">欢迎登录</h1>
          <h2 class="unselectable">Welcom Client</h2>
          <el-form-item class="account" prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
              placeholder="请输入账号"
            ></el-input>
          </el-form-item>
          <el-form-item class="password" prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
              placeholder="请输入密码"
            ></el-input>
          </el-form-item>
          <el-form-item class="login-btn">
            <el-button
              :loading="loading"
              type="primary"
              style="width: 80%; margin-left: 70px"
              @click="loginGo"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import useUserstore from '../../store/modules/user'
import { useRouter } from 'vue-router'
import { ElMessage, ElNotification, FormRules } from 'element-plus'
import getTime from '../../utils/time'
import { h } from 'vue'
let loading = ref(false)
let $router = useRouter()
let userStore = useUserstore()
let loginForm = ref({
  username: '',
  password: '',
})
/* const userInfoJson = JSON.stringify(loginForm.value)
localStorage.setItem('userInfo', userInfoJson) */
let loginForms = ref('')
//自定义校验规则函数
const validatorUsername = (rule: object, value: string, callback: any) => {
  //rule位校验规则对象
  //value为表单元素文本内容
  //函数：如果符合条件callback放行通过
  //如果不符合条件callback方法，注入错误提示信息
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号长度至少5位'))
  }
}

const validatorPassword = (rule: object, value: string, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码长度至少6位'))
  }
}

async function loginGo() {
  //点击登录按钮以后干什么
  //通知仓库发送登录请求
  //请求成功》首页展示数据的地方
  //请求失败》弹出登录失败信息
  await loginForms.value.validate() //校验表单通过后再发请求
  loading.value = true
  try {
    await userStore.userLogin(loginForm.value)
    //跳转路由
    $router.push('/')
    ElNotification({
      title: `Hi,${getTime()}`,
      message: h('i', { style: 'color: teal' }, '欢迎回来'),
    })
    loading.value = false
    /*  localStorage.setItem('username', loginForm.value.username) */
  } catch {
    ElMessage.error('账号或密码不正确')
    loading.value = false
  }
}
const rules = ref<FormRules>({
  username: [{ trigger: 'blur', validator: validatorUsername }],
  password: [{ trigger: 'blur', validator: validatorPassword }],
})
</script>

<style scoped lang="scss">
.login {
  width: 100%;
  height: 100vh;
  background: url('../../assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login-form {
    height: 300px;
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('../../assets//images//login_form.png') no-repeat;

    h1 {
      font-size: 28px;
      color: #fff;
      font-weight: 900;
      margin-left: 20px;
      padding-top: 10px;
    }

    h2 {
      font-size: 24px;
      color: #000;
      margin: 10px 0;
      margin-left: 20px;
    }

    .account {
      width: 80%;
      margin-left: 40px;
    }

    .password {
      width: 80%;
      margin-left: 40px;
    }

    .login-btn {
      width: 80%;
    }
  }
}
</style>
